/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.view.renderer
{
	import flash.events.MouseEvent;
	
	import mx.controls.Image;

	[Style(name="borderColor",type="uint",format="Color",inherit="no")]
	[Style(name="borderThickness",type="Number",format="Length",inherit="no")]
	[Style(name="borderAlpha",type="Number",format="Length",inherit="no")]

	/**
	 * @see http://www.flexer.info/2008/06/12/continuing-image-with-border/
	 */
	public class ImageBorder extends Image
	{
		
		// initial position
		private var _recalX:Number;
		private var _recalY:Number;
		
		public function ImageBorder()
		{
			super();
			_recalX = x;
			_recalY = y;
			this.setStyle("borderColor", 0xFFFFFF);
			this.setStyle("borderAlpha", 0.7);
			this.setStyle("borderThickness", 0);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			this.alpha = 0.8;
			this.setStyle("borderColor", 0x000000);
			this.setStyle("borderThickness", 1);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			this.alpha = 1;
			this.setStyle("borderColor", 0xFFFFFF);
			this.setStyle("borderThickness", 0);
		}
		
		private function onMouseDown(event:MouseEvent):void
		{
			this.alpha = 0.5;
		}
		
		private function onMouseUp(event:MouseEvent):void
		{
			this.alpha = 1;
			this.setStyle("borderColor", 0xFFFFFF);
			this.setStyle("borderThickness", 0);
		}

		// overriding the update function
		override protected function updateDisplayList(w:Number,h:Number):void
		{
			super.updateDisplayList(w,h);
			// clear graphics
			// we want only one rectangle
			graphics.clear();
			// set line style with with 0 and alpha 0
			graphics.lineStyle(getStyle('borderThickness'),getStyle('borderColor'),getStyle('borderAlpha'),false);
			// draw rectangle
			//graphics.beginFill(getStyle('borderColor'),getStyle('borderAlpha'));
			var thickness:Number = getStyle('borderThickness');
			var startX:Number;
			var startY:Number;
			var endX:Number;
			var endY:Number;
			startX = -thickness;
			endX = contentWidth + thickness * 2;
			startY = -thickness;
			endY = contentHeight + thickness * 2;
				
			graphics.drawRect(startX,startY,endX,endY);
			//graphics.endFill();
		}

	}
}